import {
    defineStore
} from 'pinia'
import {
    ref
} from "vue";
import {
    wxLogin
} from "@/src/api/login";
export const useAuthStore = defineStore('auth', () => {
    const token = ref(uni.getStorageSync("auth") || '');
    const userInfo = ref(uni.getStorageSync("userInfo") || '');
    const userRole = ref(uni.getStorageSync("userRole") || '');
    const currRole = ref(uni.getStorageSync("currRole") || '');

    function login() {
        return new Promise((resolve, reject) => {
            //角色集合
            const roles = [
                //     {
                //     name: '普通用户',
                //     value: 'null'
                // },
            ];
            uni.login({
                provider: "weixin",
                success: (res) => {
                    console.log(res)

                    wxLogin({
                        code: res.code
                    }).then(res => {
                        if (res.result) {
                            token.value = res.result.token;
                            //将用户的角色状态值整合到信息中
                            let userInfos = {
                                'coachFlag': res.result.coachFlag,
                                'stuFlag': res.result.stuFlag,
                                ...res.result.userInfo
                            };
                            userInfo.value = userInfos;
                            if (res.result.coachFlag == 1) {
                                roles.push({
                                    name: '教练',
                                    value: 'coach'
                                })
                            }
                            if (res.result.stuFlag == 1) {
                                roles.push({
                                    name: '学员',
                                    value: 'stu'
                                })
                            }
                            //更新角色数据
                            userRole.value = roles;
                            resolve(true);
                        } else {
                            reject()
                        }

                    })

                },
                fail: () => {
                    reject()
                }
            });
        })
    };

    function logout() {
        token.value = '';
        userInfo.value = '';
        userRole.value = '';
        currRole.value = {
            name: '普通用户',
            value: 0
        };
    };


    function setRole(val) {
        currRole.value = val;
    };

    function isAuthenticated() {
        return !!token.value;
    }
    return {
        token,
        userInfo,
        userRole,
        currRole,
        login,
        logout,
        setRole,
        isAuthenticated
    }
}, {
    persist: {
        persist: true //持久化
    }
})
// export const useTokenStore = defineStore("token", {
//     state: () => {
//         return {
//             token: ''
//         };
//     },
//     actions: {
//         setToken(token) {
//             this.token = token;
//         }
//     }
// })